package com.shopping.service;

import java.util.List;

import com.shopping.dao.UserDAO;
import com.shopping.exception.PasswordNotCorrectionException;
import com.shopping.exception.UserNotFoundException;
import com.shopping.modean.User;

public class UserService {

	private UserDAO userDAO = new UserDAO();
	
	/**
	 * 添加用户
	 * 
	 * @date 2018-05-29
	 */
	public void save(User user) {
		userDAO.insert(user);
		System.out.println("添加用户成功,userName:" + user.getUsername());
	}
	
	/**
	 * 用户列表
	 * 
	 * @date 2018-06-01
	 */
	public List<User> list() {
		List<User> users = userDAO.getUsers();
		System.out.println("查询用户列表,userSize:" + users.size());
		return users;
	}
	
	/**
	 * 用户删除
	 * 
	 * @date 2018-06-01
	 */
	public void delete(int id) {
		userDAO.delete(id);
		System.out.println("删除用户成功,id:" + id);
	}
	
	/**
	 * 用户登录
	 * 
	 * @date 2018-06-02
	 */
	public User login(String username,String password) throws PasswordNotCorrectionException,UserNotFoundException {
		User u = userDAO.findByUsername(username);
		if(u == null) {
			throw new UserNotFoundException();
		}
		if(!u.getPassword().equals(password)) {
			throw new PasswordNotCorrectionException();
		}
		return u;
	}
	
	/**
	 * 修改用户
	 * 
	 * @date 2018-05-29
	 */
	public void update(User user) {
		System.out.println("修改用户,userName:" + user.getUsername());
		userDAO.update(user);
	}
	

	
	

	
	
	
}
